package com.cmxh.mycloudnotes.controller;


import com.cmxh.mycloudnotes.Service.CategoryService;
import com.cmxh.mycloudnotes.pojo.Category;
import com.cmxh.mycloudnotes.util.Code01;
import com.cmxh.mycloudnotes.util.PageUtil;
import com.cmxh.mycloudnotes.util.Result01;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/coteogry")
public class CateryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/findAll")
    public Result01 findAll(@RequestBody PageUtil pageUtil) {
        PageInfo<Category> ca = categoryService.findAll( pageUtil );
        if (ca != null) {
            return new Result01( Code01.GET_OK, ca, "查询成功" );
        } else {
            return new Result01( Code01.GET_ERR, "查询失败" );
        }
    }

    @RequestMapping("/updateZt/{id}")
    public Result01 updateZt(@PathVariable Integer id) {
        System.out.println( "id = " + id );
        int i = categoryService.updateZt( id );
        if (i > 0){
            return new Result01( Code01.DELETE_OK, "删除成功" );
        }else {
            return new Result01( Code01.DELETE_ERR, "删除失败" );
        }
    }
    @RequestMapping("/save")
    public Result01 save(@RequestBody Category category) {
        int i = categoryService.save( category );
        if (i > 0){
            return new Result01( Code01.SAVE_OK, "添加成功" );
        }else {
            return new Result01( Code01.SAVE_ERR, "添加失败" );
        }
    }

    @RequestMapping("/bianji")
    public Result01 bianji(@RequestBody Category category) {
        int i = categoryService.bianji( category );
        if (i > 0){
            return new Result01( Code01.UPDATE_OK, "修改成功" );
        }else {
            return new Result01( Code01.UPDATE_ERR, "修改失败" );
        }
    }
    
}
